from django.shortcuts import render,redirect
#导包
from django.http import HttpResponse,HttpResponseRedirect,JsonResponse
#导入类视图
from django.views import View

from myapp.models import *
import json
from django.core.serializers import serialize
from rest_framework.response import Response
from rest_framework.views import APIView
#导入加密库
import hashlib
#导入图片库
#绘画库
from PIL import ImageDraw
#字体库
from PIL import ImageFont
#图片库
from PIL import Image
#随机库
import random
#文件流
import io

import requests

#导入上传文件夹配置
from mydjango.settings import UPLOAD_ROOT
import os

#导入原生sql模块
from django.db import connection

import jwt

#导入redis数据库
import redis

#导入时间模块
import time

#导入公共目录变量
from mydjango.settings import BASE_DIR

#导包
from django.db.models import Q,F

#导入dwebsocket的库
from dwebsocket.decorators import accept_websocket
import uuid

#方法视图
def myindex(request):

    return HttpResponse('这里是首页')

# 类视图
class MyView(View):


    greeting = '你好啊'

    def get(self,request):

        title = '你好 python'
        num = 100
        li = [1,2,3]
        dic = {'name':'汽车','price':100}


        return render(request,'index.html',locals())



def make_password(mypass):

    # 生成md5对象
    md5 = hashlib.md5()
    # 定义加密对象
    sign_str = str(mypass)
    # 转码
    sign_utf8 = sign_str.encode(encoding="utf-8")
    # 加密
    md5.update(sign_utf8)
    # 生成密钥串
    md5_server = md5.hexdigest()

    return md5_server


# #注册模块
# class Register(APIView):
#
# 	def get(self,request):
#
# 		#接收参数  dict['username']
# 		username = request.GET.get('username','null')
# 		password = request.GET.get('password','null')
#
# 		print(username)
# 		print(password)
#
# 		#排重
# 		user = User.objects.filter(username=username).first()
#
# 		if user:
# 			res = {}
# 			res['code'] = 405
# 			res['message'] = '该用户名已存在'
# 			return Response(res)
#
# 		#入库
# 		user = User(username=username,password=make_password(password))
# 		user.save()
#
# 		res = {}
# 		res['code'] = 200
# 		res['message'] = '恭喜，注册成功'
#
# 		return Response(res)





class AddUser(View):
    def get(self, request):
        return render(request, 'addUser.html')

    msg = ''

    def post(self, request):
        username = request.POST.get('username')
        password = request.POST.get('password')
        img = request.FILES.get('img')
        type = request.POST.get('type')
        User.objects.create(username=username, password=password, img=img, type=type)
        msg = '添加成功'
        return redirect('/', locals())





# 展示所有用户
class AllUser(View):
    def get(self,request):
        users=User.objects.all()
        return render(request,'user.html',locals())




#修改操作
class Update(View):
    def get(self,request):
        id=request.GET.get('id')
        users=User.objects.filter(pk=id)
        return render(request, 'upload.html', locals())

    def post(self, request):
        id = request.GET.get('id')
        username = request.POST.get('username')
        password = request.POST.get('password')
        type = request.POST.get('type')
        User.objects.filter(pk=id).update(username=username, password=password, type=type)

        return redirect('/')


#删除操作
class Delete(View):
    def get(self,request):
        id=request.GET.get('id')
        User.objects.filter(id=id).delete()
        return redirect('/')